﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataGridViewAutoFilter;

namespace ECare.Client.controls
{
    public class EGridView : DataGridView
    {
        public EGridView()
        {
            this.RowPostPaint += EGridView_RowPostPaint;

            this.AutoGenerateColumns = false;
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            this.BackgroundColor = System.Drawing.Color.White;

            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.AllowUserToAddRows = false;
            this.AllowUserToDeleteRows = false;


            this.RowsDefaultCellStyle.BackColor = Color.White;
            this.AlternatingRowsDefaultCellStyle.BackColor = Color.Gainsboro;

            this.MultiSelect = false;
            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;


            this.RowTemplate.Height = 50;
            this.ColumnHeadersHeight = 50;

            this.BorderStyle = BorderStyle.None;

            this.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma",14);

            //    ColumnHeadersDefaultCellStyle.BackColor = Color.White;


    

        }

        void EGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
          
        
        }

        void EGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up))
            {
                DataGridViewAutoFilterColumnHeaderCell filterCell =
                    CurrentCell.OwningColumn.HeaderCell as
                    DataGridViewAutoFilterColumnHeaderCell;
                if (filterCell != null)
                {
                    filterCell.ShowDropDownList();
                    e.Handled = true;
                }
            }
        }

        void EGridView_BindingContextChanged(object sender, EventArgs e)
        {
            if (DataSource == null)
            {
                return;
            }

            // Add the AutoFilter header cell to each column.
            foreach (DataGridViewColumn col in Columns)
            {
                col.HeaderCell = new
                    DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
            }



            // Resize the columns to fit their contents.
            AutoResizeColumns();
        }

        void EGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //throw new NotImplementedException();
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                this.RowHeadersDefaultCellStyle.Font, rectangle,
                this.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}
